﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

namespace InpValidator
{
	public class ValidatorManager : FrameworkElement
	{
		public class ManagerList
		{
			public FrameworkElement Element { get; set; }
			public ValidatorBase Validator { get; set; }
		}

		List<ManagerList> items = new List<ManagerList>();

        public void Register(ValidatorBase validator)
        {
            items.Add(new ManagerList() { Element = validator.ElementToValidate, Validator = validator });
        }

        /// <summary>
        /// Validate all validators within the group
        /// </summary>
        /// <returns></returns>
		public List<ValidatorBase> ValidateAll()
		{
			List<ValidatorBase> errors = new List<ValidatorBase>();

            List<ManagerList> temp = new List<ManagerList>();

            foreach (var item in items)
            {
                temp.Add(new ManagerList
                {
                    Element = item.Element,
                    Validator = item.Validator
                });
            }

            foreach (var item in temp)
			{
				if ((!item.Validator.Validate()) && (item.Validator.Type == ValidatorType.Error))
				{
					errors.Add(item.Validator);
				}
			}
			return errors;
		}

		public static string GetErrorString(IEnumerable<string> list, string delimiter)
		{
			string s = "";
			foreach (var item in list)
			{
				s += item + delimiter;
			}
			return s.TrimEnd(delimiter.ToCharArray());
		}

		public static string GetErrorString(IEnumerable<ValidatorBase> list, string delimiter)
		{
			return GetErrorString(
				list.Select(n => n.ErrorMessage).ToList(), delimiter);
		}
	}

}
